﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FSToolkit
{
    class PreprocessData
    {
        internal Dictionary<string, int> DitLevel { get; private set; }
        readonly Random rd;
        public PreprocessData()
        {
            DitLevel = new Dictionary<string, int>();
            rd = new Random();
        }

        public DataTable PreprocessDataTabel(DataTable data, List<string> Levels)
        {
            var dt = data.Copy();
            if (dt.Columns.Count != 3 || dt.Columns[0].DataType != typeof(float) || dt.Columns[1].DataType != typeof(string) || dt.Columns[2].DataType != typeof(string))
                throw new Exception("Data source mismatch ! The first three columns’type must be float,string and string");
            var value = dt.Columns[0].ColumnName;
            var level = dt.Columns[1].ColumnName;
            var id = dt.Columns[2].ColumnName;
            dt.Columns.Add("Index", typeof(int));
            dt.Columns["Index"].SetOrdinal(0);
            dt.Columns[value].SetOrdinal(1);
            dt.Columns[level].SetOrdinal(2);
            dt.Columns[id].SetOrdinal(3);
            var index = 1;
            DitLevel.Clear();
            Levels.ForEach(x => DitLevel.Add(x, 100 * (index++)));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][0] = DitLevel[dt.Rows[i][2].ToString()] + GetRandom();
            }
            return dt;
        }
        private int GetRandom()
        {
            var v = rd.Next(0, 21);
            return 10 - v;
        }
    }
}
